package cn.ekuma.data.ui.android.editor;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.BrowseListener;
import com.openbravo.data.user.I_AndroidBrowsableEditableData;
import com.openbravo.data.user.I_BrowsableEditableData;

import com.openbravo.data.user.StateListener;

import cn.ekuma.data.ui.android.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class JNavigator extends LinearLayout implements BrowseListener, StateListener {
    private ImageButton jbtnFirst,jbtnPrev,jbtnNext,jbtnLast;
    I_AndroidBrowsableEditableData m_bd;
	
	public JNavigator(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	 public JNavigator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public JNavigator(Context context) {
		super(context);
		init(context);
	}

	private void init(final Context context) {
	        View mainView = LayoutInflater.from(context).inflate( 
	                        R.layout.navigator1, this); 
	        jbtnFirst=(ImageButton) mainView.findViewById(R.id.btn_Top);
	        jbtnFirst.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					jbtnFirstActionPerformed();	
				}
	        	
	        });
	        jbtnPrev=(ImageButton) mainView.findViewById(R.id.btn_Pre);
	        jbtnPrev.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					jbtnPrevActionPerformed();	
				}
	        	
	        });
	        jbtnNext=(ImageButton) mainView.findViewById(R.id.btn_Next);
	        jbtnNext.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					jbtnNextActionPerformed();	
				}
	        	
	        });
	        jbtnLast=(ImageButton) mainView.findViewById(R.id.btn_End);
	        jbtnLast.setOnClickListener(new OnClickListener(){

				public void onClick(View v) {
					jbtnLastActionPerformed();	
				}
	        	
	        });
	 }
	
	
	public void setAndroidBrowsableEditableData(I_AndroidBrowsableEditableData db){
		this.m_bd=db;
		m_bd.addBrowseListener(this);
		m_bd.addStateListener(this);
	}
	public void updateState(int iState) {
        if (iState == I_BrowsableEditableData.ST_INSERT || iState == I_BrowsableEditableData.ST_DELETE) {
             // Insert o Delete
            if (jbtnFirst != null) jbtnFirst.setEnabled(false);
            if (jbtnPrev != null) jbtnPrev.setEnabled(false);
            if (jbtnNext != null) jbtnNext.setEnabled(false);
            if (jbtnLast != null) jbtnLast.setEnabled(false);
            //if (jbtnRefresh != null) jbtnRefresh.setEnabled(true);
        }
    }  
    
    public void updateIndex(int iIndex, int iCounter) {
        
        if (iIndex >= 0 && iIndex < iCounter) {
            // Reposicionamiento
            if (jbtnFirst != null) jbtnFirst.setEnabled(iIndex > 0);
            if (jbtnPrev != null) jbtnPrev.setEnabled(iIndex > 0);
            if (jbtnNext != null) jbtnNext.setEnabled(iIndex < iCounter - 1);
            if (jbtnLast != null) jbtnLast.setEnabled(iIndex < iCounter - 1);
            //if (jbtnRefresh != null) jbtnRefresh.setEnabled(true);
        } else {
            // EOF
            if (jbtnFirst != null) jbtnFirst.setEnabled(false);
            if (jbtnPrev != null) jbtnPrev.setEnabled(false);
            if (jbtnNext != null) jbtnNext.setEnabled(false);
            if (jbtnLast != null) jbtnLast.setEnabled(false);
            //if (jbtnRefresh != null) jbtnRefresh.setEnabled(false);
        }
    }   

    private void jbtnRefreshActionPerformed() {                                              
        m_bd.actionReloadCurrent(this.getContext());       
    }                                           

    private void jbtnReloadActionPerformed() {                                           
        try {
            m_bd.actionLoad();
        } catch (BasicException eD) {
           
        }
    }                                          

    private void jbtnLastActionPerformed() {                                         

        try {
            m_bd.moveLast();
        } catch (BasicException eD) {
           eD.printStackTrace();
        }
    }                                        

    private void jbtnFirstActionPerformed() {                                          

        try{
            m_bd.moveFirst();
        } catch (BasicException eD) {
            eD.printStackTrace();
        }
    }                                         

    private void jbtnPrevActionPerformed() {                                         
        try {
            m_bd.movePrev();
        } catch (BasicException eD) {
            eD.printStackTrace();
        }       
    }                                        

   
    private void jbtnNextActionPerformed() {
        try {
            m_bd.moveNext();
        } catch (BasicException eD) {
            eD.printStackTrace();
        }     
    }                                        
	
}
